#!/usr/bin/env python3

import argparse
import os
import os.path
import platform
import re
import shutil
import stat
import subprocess
import sys
import urllib.request
from typing import Any, Dict, List, Optional

import yaml


def run(args: argparse.Namespace, command: List[str], **kwargs: Any) -> None:
    if args.verbose or args.dry_run:
        print(" ".join(command))
    if not args.dry_run:
        subprocess.run(command, **kwargs)  # nosec


def main() -> None:
    parser = argparse.ArgumentParser(description="Build the project")
    parser.add_argument("--verbose", action="store_true", help="Display the docker build commands")
    parser.add_argument(
        "--dry-run", action="store_true", help="Display the docker build commands without executing them"
    )
    parser.add_argument("--service", help="Build only the specified service")
    parser.add_argument("--env", action="store_true", help="Build only the .env file")
    parser.add_argument("--simple", action="store_true", help="Force simple application mode")
    parser.add_argument("--not-simple", action="store_true", help="Force not simple application mode")
    parser.add_argument("--upgrade", help="Start upgrading the project to version")
    parser.add_argument(
        "--fast-reload",
        action="store_true",
        help="Restart the composition without Redis to don't lost the cache",
    )
    parser.add_argument(
        "--no-pull",
        action="store_true",
        default=os.environ.get("CI", "FALSE").upper() == "TRUE",
        help="Do not pull external or base images for faster rebuild during development.",
    )
    parser.add_argument(
        "--debug", help="Path to c2cgeoportal source folder to be able to debug the upgrade procedure"
    )
    parser.add_argument(
        "--docker-compose-version-2", action="store_true", help="Use Docker Compose version 2"
    )
    parser.add_argument("env_files", nargs="*", help="The environment config")
    args = parser.parse_args()

    if args.upgrade:
        major_version = args.upgrade
        match = re.match(r"^([0-9]+\.[0-9]+)\.[0-9]+$", args.upgrade)
        if match is not None:
            major_version = match.group(1)
        match = re.match(r"^([0-9]+\.[0-9]+)\.[0-9a-z]+\.[0-9]+$", args.upgrade)
        if match is not None:
            major_version = match.group(1)
        full_version = args.upgrade if args.upgrade != "master" else "latest"
        with open("upgrade", "w", encoding="utf-8") as f:
            with urllib.request.urlopen(  # nosec
                "https://raw.githubusercontent.com/camptocamp/c2cgeoportal/{major_version}/scripts/upgrade".format(
                    major_version=major_version
                )
            ) as result:
                if result.code != 200:
                    print("ERROR:")
                    print(result.read())
                    sys.exit(1)
                f.write(result.read().decode())
        debug_args = []
        if args.debug:
            shutil.copyfile(os.path.join(args.debug, "scripts", "upgrade"), "upgrade")
            debug_args = ["--debug", args.debug]
        os.chmod("upgrade", os.stat("upgrade").st_mode | stat.S_IXUSR)
        try:
            if platform.system() == "Windows":
                run(args, ["python", "upgrade", full_version] + debug_args, check=True)
            else:
                run(args, ["./upgrade", full_version] + debug_args, check=True)
        except subprocess.CalledProcessError:
            sys.exit(1)
        sys.exit(0)

    with open("project.yaml", encoding="utf-8") as project_file:
        project_env = yaml.load(project_file, Loader=yaml.SafeLoader)["env"]
    if len(args.env_files) != project_env["required_args"]:
        print(project_env["help"])
        sys.exit(1)
    env_files = [e.format(*args.env_files) for e in project_env["files"]]
    print("Use env files: {}".format(", ".join(env_files)))
    for env_file in env_files:
        if not os.path.exists(env_file):
            print("Error: the env file '{env_file}' does not exist.".format(env_file=env_file))
            sys.exit(1)

    with open(".env", "w", encoding="utf-8") as dest:
        for file_ in env_files:
            with open(file_, encoding="utf-8") as src:
                dest.write(src.read() + "\n")

            simple = not os.path.exists("geoportal/Dockerfile")
            if args.simple:
                simple = True
            if args.not_simple:
                simple = False

            git_hash = (
                subprocess.run(["git", "rev-parse", "HEAD"], check=True, stdout=subprocess.PIPE)
                .stdout.strip()
                .decode()
            )

            dest.write("SIMPLE={}\n".format(str(simple).upper()))
            dest.write("GIT_HASH={git_hash}\n".format(git_hash=git_hash))

        dest.write("# Used env files: {}\n".format(", ".join(env_files)))

    docker_compose = ["docker", "compose"] if args.docker_compose_version_2 else ["docker-compose"]
    if not args.env:
        docker_compose_build_cmd = [*docker_compose, "build"]

        if not args.no_pull:
            # Pull all the images
            if not args.service:
                run(args, [*docker_compose, "pull", "--ignore-pull-failures"], check=True)  # nosec
            docker_compose_build_cmd.append("--pull")

        if args.service:
            docker_compose_build_cmd.append(args.service)

        print_args = [a.replace(" ", "\\ ") for a in docker_compose_build_cmd]
        print_args = [a.replace('"', '\\"') for a in print_args]
        print_args = [a.replace("'", "\\'") for a in print_args]
        try:
            run(args, docker_compose_build_cmd, check=True)  # nosec
        except subprocess.CalledProcessError as e:
            print("Error with command: " + " ".join(print_args))
            sys.exit(e.returncode)

    if args.fast_reload:
        services = [
            service
            for service in subprocess.run(
                [*docker_compose, "ps", "--services", "--all"], stdout=subprocess.PIPE, check=True
            )
            .stdout.decode()
            .splitlines()
            if not service.startswith("redis")
        ]

        run(args, [*docker_compose, "rm", "--stop", "--force"] + services, check=True)
        run(args, [*docker_compose, "up", "-d"], check=True)


if __name__ == "__main__":
    main()
